﻿using BibleBase;
using MarkupParser;
using NVI_LIB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Util;
using System.Text.RegularExpressions;

namespace TEST
{
    class Program
    {
        #region Loading and sorting

        static Bible LoadNVI()
        {
            return BibleLoader.LoadBible("NVI", Config.RootPath + @"\NVI.bin");
        }

        static Bible LoadAndSortNVI()
        {
            Bible nvi = LoadNVI();
            BibleLoader.SortBible(nvi);
            return nvi;
        }

        static Bible LoadSortAndDumpNVI()
        {
            Bible nvi = LoadAndSortNVI();

            string nviFile = Config.RootPath + @"\NVI.txt";
            if (File.Exists(nviFile))
                File.Delete(nviFile);
            File.AppendAllText(nviFile, nvi.ToString());

            return nvi;
        }

        static Bible LoadRV()
        {
            return BibleLoader.LoadBible("RVR1960", Config.RootPath + @"\RVR1960.bin");
        }

        static Bible LoadAndSortRV()
        {
            Bible rv = LoadRV();
            BibleLoader.SortBible(rv);
            return rv;
        }

        static Bible LoadSortAndDumpRV()
        {
            Bible rv = LoadAndSortRV();

            string rvFile = Config.RootPath + @"\RVR1960.txt";
            if (File.Exists(rvFile))
                File.Delete(rvFile);
            File.AppendAllText(rvFile, rv.ToString());

            return rv;
        }

        #endregion

        #region Integrity and coherence

        static void LoadAndCompareBibles()
        {
            Bible nvi = BibleLoader.LoadBible("NVI", Config.RootPath + @"\NVI.bin");
            BibleLoader.SortBible(nvi);
            Bible rv = BibleLoader.LoadBible("RVR1960", Config.RootPath + @"\RVR1960.bin");
            BibleLoader.SortBible(rv);

            Bible.CompareBibles(nvi, rv);

            nvi.Save(Config.RootPath + @"\NVI.bin");
            rv.Save(Config.RootPath + @"\RVR1960.bin");
        }

        static void VerifyRVIntegrity()
        {
            Bible rv = BibleLoader.LoadBible("RVR1960", Config.RootPath + @"\RVR1960.bin");
            //BibleLoader.SortBible(rv);

            rv.VerifyIntegrity();
            rv.Save(Config.RootPath + @"\RVR1960.bin");
        }

        static void VerifyNVIIntegrity()
        {
            Bible nvi = BibleLoader.LoadBible("NVI", Config.RootPath + @"\NVI.bin");
            //BibleLoader.SortBible(nvi);

            nvi.VerifyIntegrity();
            nvi.Save(Config.RootPath + @"\NVI.bin");
        }

        static void TestNVIParentheses()
        {
            Regex rx = new Regex(@"\(\d", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Bible nvi = LoadNVI();
            nvi.Books.ForEach(book =>
            {
                book.Chapters.ForEach(chapter =>
                {
                    chapter.Verses.ForEach(verse =>
                    {
                        MatchCollection matches = rx.Matches(verse.Text);
                        if (matches.Count > 0)
                            Logger.WriteLine("{0} {1}:{2} -> {3}", book.Name, chapter.Number, verse.Number, verse.Text);
                    });
                });
            });
        }

        static void TestRVParentheses()
        {
            Regex rx = new Regex(@"\(\d", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Bible nvi = LoadRV();
            nvi.Books.ForEach(book =>
            {
                book.Chapters.ForEach(chapter =>
                {
                    chapter.Verses.ForEach(verse =>
                    {
                        MatchCollection matches = rx.Matches(verse.Text);
                        if (matches.Count > 0)
                            Logger.WriteLine("{0} {1}:{2} -> {3}", book.Name, chapter.Number, verse.Number, verse.Text);
                    });
                });
            });
        }

        #endregion

        #region Replacements

        static Abbreviations LoadAbbreviations()
        {
            return BibleLoader.LoadAbreviaturas(Config.RootPath + @"\abreviaturas.xlsx");
        }

        static void DoReplacements(List<string> filenames)
        {
            //LoadAndCompareBibles();

            Abbreviations abreviaturas = LoadAbbreviations();

            //Bible nvi = LoadAndSortNVI();
            //Bible rv = LoadAndSortRV();

            Bible nvi = LoadNVI();
            Bible rv = LoadRV();

            //if (File.Exists(Config.RootPath + @"\IndiceDeTemasSample.docx")) File.Delete(Config.RootPath + @"\IndiceDeTemasSample.docx");
            //File.Copy(Config.RootPath + @"\IndiceDeTemasSample1.docx", Config.RootPath + @"\IndiceDeTemasSample.docx");

            filenames.ForEach(filename =>
                {

                    IndiceDeTemasParser parser =
                        new IndiceDeTemasParser(
                            Config.RootPath + filename,//@"\IndiceDeTemasSample.docx",
                            Config.RootPath + @"\BIN\MapeoVocabularioTemas.bin",
                            Config.RootPath + @"\BIN\ModifEntradas.bin", nvi, rv, abreviaturas);

                    ////parser.ParseEntries();
                    ////parser.ReplaceEntries();
                    //#if _
                    NVIRVEntriesMap map = BibleLoader.LoadNVIRVEntriesMap(Config.RootPath + @"\1.xls", Config.RootPath + @"\2.xls");
                    parser.ReplaceEntriesWithRegex(map.RVNumberFromNVINumber);

                    ////parser.ParseVerses();
                    ////parser.CountVerses();
                    ////List<Fragment> fragments = parser.ParseExactVerses();

                    List<Fragment> fragments = parser.ParseVersesWithRegex();

                    ////parser.HighlightVerses();
                    ////parser.ReplaceVerses(fragments);

                    EntradasFinal entFinal = BibleLoader.LoadEntradasFinalMap(Config.RootPath + @"\Entradas (final!).xls");
                    parser.ReplaceVease(entFinal.RVNumberFromNVINumber);
                    //#endif
                    parser.ReplaceWords();

                    parser.Close();
                });
        }

        #endregion

        #region Double excalamtion

        static void FixDoubleExclamation(Bible bible)
        {
            bible.Books.ForEach(book => book.Chapters.ForEach(chapter => chapter.Verses.ForEach(verse => { if (verse.Text.Contains("!!")) verse.Text.Replace("!!", "¡"); })));
        }

        static void FixDoubleExclamation()
        {
            Bible rv = LoadRV();
            Bible nvi = LoadNVI();
            FixDoubleExclamation(rv);
            FixDoubleExclamation(nvi);
            rv.Save();
            nvi.Save();
        }

        #endregion

        static void Main(string[] args)
        {
            SysUtils.KillWord();

            //DoReplacements(new List<string> { @"\IndiceDeTemasSample.docx" });
            //DoReplacements(new List<string> { @"\parte001.docx" });
            //DoReplacements(new List<string> { @"\parte002.docx" });
            //DoReplacements(new List<string> { @"\parte003.docx" });
            //DoReplacements(new List<string> { @"\parte004.docx" });
            DoReplacements(new List<string> { @"\parte005.docx" });

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
    }
}
